package com.aooci.ordercat.desktop.view;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import com.aooci.ordercat.desktop.OrdercatMain;
import com.aooci.ordercat.vo.CheckoutVo;
import com.aooci.ordercat.vo.OperatedOrderVo;

@SuppressWarnings("restriction")
public class ClearOrderController implements Initializable {
	private OrdercatMain ordercatMain;
	private OperatedOrderVo operatedOrderVo;
	private CheckoutVo checkoutVo;
	//private Label tableNumber;
	@FXML private Label total;
	@FXML private Label tip;
	@FXML private Label offert;
	@FXML private Label discountRate;
	@FXML private Label discount;
	@FXML private Label totalToPay;
	@FXML private Label inputAmount;
	@FXML private Label mentionAmount;
	@FXML private Label missingAmount;
	@FXML private Label givingChangeAmount;
	
	@FXML private TextField cash;
	@FXML private TextField card;
	@FXML private TextField cheque;
	
	@FXML private Button confirm;
	@FXML private Button close;

	public void initialize(URL arg0, ResourceBundle arg1) {
		this.totalToPay.setText("100.00");
		
		this.close.setOnAction((event) -> {
			((Stage) this.close.getScene().getWindow()).close();
		});
		
		this.confirm.setOnAction((event) -> {
			this.checkoutVo.setCard(Float.parseFloat(card.getText()));
			this.checkoutVo.setCash(Float.parseFloat(cash.getText()));
			this.checkoutVo.setCheque(Float.parseFloat(cheque.getText()));
			ordercatMain.getOrderOperationService().clearOrder(this.operatedOrderVo, this.checkoutVo);
			((Stage) this.confirm.getScene().getWindow()).close();
		});
		
		this.cash.setOnMouseClicked((event) -> {
			cash.selectAll();
		});
		this.card.setOnMouseClicked((event) -> {
			card.selectAll();
		});
		this.cheque.setOnMouseClicked((event) -> {
			cheque.selectAll();
		});
		this.cash.setOnKeyReleased((event) -> {
			validateInputAmount();
		});
		this.card.setOnKeyReleased((event) -> {
			validateInputAmount();
		});
		this.cheque.setOnKeyReleased((event) -> {
			validateInputAmount();
		});
	}
	
	public void init(OrdercatMain ordercatMain, OperatedOrderVo operatedOrderVo, CheckoutVo checkoutVo){
		this.ordercatMain = ordercatMain;
		this.operatedOrderVo = operatedOrderVo;
		this.checkoutVo = checkoutVo;
		
		this.total.setText(String.valueOf(this.operatedOrderVo.getOrdercat().getTotal()));
		this.tip.setText(String.valueOf(this.checkoutVo.getTip()));
		this.offert.setText(String.valueOf(this.checkoutVo.getOffert()));
		this.discountRate.setText(String.valueOf(this.checkoutVo.getDiscountRate()));
		this.discount.setText(String.valueOf(this.checkoutVo.getDiscount()));
		this.totalToPay.setText(String.valueOf(this.checkoutVo.getTotalToPay()));
	}
	
	private void validateInputAmount(){
		float inputAmount = Float.parseFloat(cash.getText()) + Float.parseFloat(card.getText()) + Float.parseFloat(cheque.getText());
		this.inputAmount.setText(String.valueOf(inputAmount));
		
		this.confirm.setDisable(true);
		this.mentionAmount.setVisible(true);
		this.mentionAmount.setText(String.valueOf(Float.parseFloat(String.valueOf(this.totalToPay.getText())) - inputAmount));
		this.givingChangeAmount.setVisible(true);
		this.missingAmount.setVisible(true);
		
		if(inputAmount > Float.parseFloat(String.valueOf(this.totalToPay.getText()))){
			this.missingAmount.setVisible(false);
			
		}else if(Float.parseFloat(String.valueOf(this.totalToPay.getText())) > inputAmount){
			this.givingChangeAmount.setVisible(false);

		}else if(inputAmount == Float.parseFloat(String.valueOf(this.totalToPay.getText()))){
			this.confirm.setDisable(false);
			this.mentionAmount.setVisible(false);
			this.missingAmount.setVisible(false);
			this.givingChangeAmount.setVisible(false);
			//this.inputAmount.setTextFill(Color.GREEN);
		}
	}
	
}
